import base


class BordaVoting(base.AbstractVotingMethod):
  """Implements Borda's method of voting."""

  def __init__(self):
    pass

  def name(self) -> str:
    return "borda"

  def run_election(self, profile: base.PreferenceProfile) -> base.RankOutcome:
    assert self.is_valid_profile(profile)
    scores = {}
    for alternative in profile.alternatives:
      scores[alternative] = 0
    for vote in profile.votes:
      # Do we need a check here for the length of the vote?
      points = len(vote.vote) - 1
      for alternative in vote.vote:
        scores[alternative] += (points * vote.weight)
        points -= 1
      assert points == -1
    sorted_scores = sorted(scores.items(), key=lambda item: item[1],
                           reverse=True)
    outcome = base.RankOutcome()
    outcome.unpack_from(sorted_scores)
    return outcome
